package web.weibo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import web.weibo.mapper.MapperUser;
import web.weibo.model.Comment;
import web.weibo.model.User;
import web.weibo.model.Weibo;
import web.weibo.service.ServiceWeibo;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class ControllerWeibo {

    @Autowired
    ServiceWeibo serviceWeibo;
    @Autowired
    MapperUser mapperUser;

    @GetMapping("/weibo/timeline")
    public ModelAndView timeline(HttpServletRequest request) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        ArrayList<User> followees = this.serviceWeibo.timelineWeibos(currentUser);
        int countCares = this.serviceWeibo.countCares(currentUser.id);
        // 读出 模板 文件并把数据放进 html 文件
        ModelAndView modelAndView = new ModelAndView("timeline");
        modelAndView.addObject("followees", followees);
        modelAndView.addObject("countCares", countCares);
        modelAndView.addObject("currentUser", currentUser);
        return modelAndView;
    }

    @GetMapping("/weibo/myFans")
    public ModelAndView Fans(HttpServletRequest request) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        ArrayList<User> fans = this.serviceWeibo.beTimelineWeibos(currentUser);
        int countBeCares = this.serviceWeibo.countBeCares(currentUser.id);
        // 读出 模板 文件并把数据放进 html 文件
        ModelAndView modelAndView = new ModelAndView("my_fans");
        modelAndView.addObject("fans", fans);
        modelAndView.addObject("countBeCares", countBeCares);
        modelAndView.addObject("currentUser", currentUser);
        return modelAndView;
    }

    @GetMapping("/weibo/follow")
    public ModelAndView follow(HttpServletRequest request, String userId) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.follow(currentUser.id, Integer.parseInt(userId));
        return new ModelAndView("redirect:/weibo/timeline");
    }

    @GetMapping("/weibo/followOver")
    public ModelAndView followOver(HttpServletRequest request, String userId) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.followOver(currentUser.id, Integer.parseInt(userId));
        return new ModelAndView("redirect:/weibo/timeline");
    }

    @GetMapping("/weibo/overFans")
    public ModelAndView overFans(HttpServletRequest request, String userId) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.overFans(currentUser.id, Integer.parseInt(userId));
        return new ModelAndView("redirect:/weibo/myFans");
    }

    @GetMapping("/myComments")
    public ModelAndView myComments(HttpServletRequest request) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        ArrayList<Weibo> weibos = this.serviceWeibo.myComments(currentUser.id);
        int comments = this.serviceWeibo.myAllComments(currentUser.id);
        int countCares = this.serviceWeibo.countCares(currentUser.id);
        int countBeCares = this.serviceWeibo.countBeCares(currentUser.id);
        ModelAndView modelAndView = new ModelAndView("myComment");
        modelAndView.addObject("weibos", weibos);
        modelAndView.addObject("comments", comments);
        modelAndView.addObject("currentUser", currentUser);
        modelAndView.addObject("countCares", countCares);
        modelAndView.addObject("countBeCares", countBeCares);
        return modelAndView;
    }

    @GetMapping("/weibo/now")
    public ModelAndView weiboNow(HttpServletRequest request, int weiboId) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        Weibo weibo = this.serviceWeibo.weiboNow(weiboId);
        int countComment = this.serviceWeibo.countComment(weiboId);
        ModelAndView modelAndView = new ModelAndView("weiboNow");
        modelAndView.addObject("weibo", weibo);
        modelAndView.addObject("currentUser", currentUser);
        modelAndView.addObject("countComment", countComment);
        return modelAndView;
    }

    @GetMapping("/weibo/all")
    public ModelAndView all(HttpServletRequest request, String userId) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        if (userId == null) {
            ArrayList<Weibo> weibos = this.serviceWeibo.allWeibos();
            ModelAndView modelAndView = new ModelAndView("weibo_all");
            modelAndView.addObject("weibos", weibos);
            modelAndView.addObject("currentUser", currentUser);
            return modelAndView;
        } else if (Integer.parseInt(userId) == currentUser.id) {
            return new ModelAndView("redirect:/weibo/simple");
        } else {
            User user = this.serviceWeibo.userWeibo(Integer.parseInt(userId));
            ArrayList<Weibo> weibos = this.serviceWeibo.currentUserWeibos(Integer.parseInt(userId));
            int countWeibo = this.serviceWeibo.countWeibos(Integer.parseInt(userId));
            int countCares = this.serviceWeibo.countCares(Integer.parseInt(userId));
            int iscare = this.serviceWeibo.iscare(currentUser.id, user.id);
            int countBeCares = this.serviceWeibo.countBeCares(Integer.parseInt(userId));
            ModelAndView modelAndView = new ModelAndView("user_weibo");
            modelAndView.addObject("weibos", weibos);
            modelAndView.addObject("currentUser", currentUser);
            modelAndView.addObject("countCares", countCares);
            modelAndView.addObject("countBeCares", countBeCares);
            modelAndView.addObject("user", user);
            modelAndView.addObject("iscare", iscare);
            modelAndView.addObject("countWeibo", countWeibo);
            return modelAndView;
        }
    }

    @GetMapping("/weibo/simple")
    public ModelAndView simple(HttpServletRequest request) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        ArrayList<Weibo> weibos = this.serviceWeibo.currentUserWeibos(currentUser.id);
        int countWeibo = this.serviceWeibo.countWeibos(currentUser.id);
        int countCares = this.serviceWeibo.countCares(currentUser.id);
        int countBeCares = this.serviceWeibo.countBeCares(currentUser.id);
        ModelAndView modelAndView = new ModelAndView("weibo_simple");
        modelAndView.addObject("weibos", weibos);
        modelAndView.addObject("countWeibo", countWeibo);
        modelAndView.addObject("countCares", countCares);
        modelAndView.addObject("countBeCares", countBeCares);
        modelAndView.addObject("currentUser", currentUser);
        return modelAndView;
    }

    @PostMapping("/weibo/add")
    public ModelAndView add(String content, HttpServletRequest request) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.add(content, currentUser);
        return new ModelAndView("redirect:/weibo/all");
    }

    @GetMapping("/comment/new")
    public ModelAndView commentNew(int id) {
        ModelAndView modelAndView = new ModelAndView("comment_new");
        modelAndView.addObject("weibo_id", id);
        return modelAndView;
    }

    @GetMapping("/comment/add")
    public ModelAndView commentAdd(HttpServletRequest request, int weiboId, String content) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.commentAdd(content, currentUser, weiboId);
        return new ModelAndView("redirect:/weibo/now?weiboId=" + weiboId);
    }

    @GetMapping("/weibo/delete")
    public ModelAndView delete(HttpServletRequest request, int id) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.delete(id, currentUser);
        return new ModelAndView("redirect:/weibo/simple");
    }

    @GetMapping("/comment/delete")
    public ModelAndView commentDelete(HttpServletRequest request, int id) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.commentDelete(id, currentUser);
        return new ModelAndView("redirect:/myComments");
    }

    @GetMapping("/weibo/edit")
    public ModelAndView edit(HttpServletRequest request, int id) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        Weibo weibo = this.serviceWeibo.weiboNow(id);
        ModelAndView modelAndView = new ModelAndView("weibo_edit");
        modelAndView.addObject("weibo_id", id);
        modelAndView.addObject("weibo", weibo);
        modelAndView.addObject("currentUser", currentUser);
        return modelAndView;
    }

    @GetMapping("/comment/edit")
    public ModelAndView commentEdit(HttpServletRequest request, int id) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        Comment comment = this.serviceWeibo.comment(id);
        ModelAndView modelAndView = new ModelAndView("comment_edit");
        modelAndView.addObject("comment_id", id);
        modelAndView.addObject("comment", comment);
        modelAndView.addObject("currentUser", currentUser);
        return modelAndView;
    }

    @GetMapping("/weibo/update")
    public ModelAndView update(HttpServletRequest request, int id, String content) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.update(id, content, currentUser);
        return new ModelAndView("redirect:/weibo/simple");
    }

    @GetMapping("/comment/update")
    public ModelAndView commentUpdate(HttpServletRequest request, int id, String content) {
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        this.serviceWeibo.commentUpdate(id, content, currentUser);
        return new ModelAndView("redirect:/myComments");
    }
}
